package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.ExportDao;
import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service(timeout = 300000)
public class ShippingServiceImpl implements ShippingService {

    @Autowired
    private ShippingDao shippingDao;
    @Autowired
    private PackingDao packingDao;
    @Autowired
    private ExportDao exportDao;
    @Autowired
    private UserDao userDao;

    @Override
    public Shipping findById(String id) {
        return shippingDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Shipping shipping) {
        //shipping.setShippingOrderId(UUID.randomUUID().toString());
        shipping.setState(0);
        shippingDao.insertSelective(shipping);
    }

    @Override
    public void update(Shipping shipping) {
        shippingDao.updateByPrimaryKeySelective(shipping);
    }

    @Override
    public void delete(String id) {
        Shipping shipping = shippingDao.selectByPrimaryKey(id);
        String shippingOrderId = shipping.getShippingOrderId();
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria criteria = packingExample.createCriteria();
        criteria.andPackingListIdEqualTo(shippingOrderId);
        List<Packing> packingList = packingDao.selectByExample(packingExample);
        for (Packing packing : packingList) {
            packing.setState(0);
            packingDao.updateByPrimaryKeySelective(packing);
        }
        shippingDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Shipping> findByPage(ShippingExample shippingExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> list = shippingDao.selectByExample(shippingExample);
        return new PageInfo<>(list);
    }

    @Override
    public Shipping PageBean(String id) {

        //委托单和装箱单公用id，根据id获取报运单的信用证号
        Packing packing = packingDao.selectByPrimaryKey(id);
        //获取装箱单中的报运id
        String exportIds = packing.getExportIds();
        String[] exportsId = exportIds.split(",");
        List<String> exportsIdList = Arrays.asList(exportsId);
        ExportExample exportExample = new ExportExample();
        ExportExample.Criteria criteria = exportExample.createCriteria();
        criteria.andIdIn(exportsIdList);
        //根据报运id获取报运单中的信用证号
        String lcNo = "";
        //根据报运id获取报运单中的装运港
        String shipmentPort = "";
        //根据报运id获取报运单中的目的港
        String destinationPort = "";
        List<Export> exportList = exportDao.selectByExample(exportExample);
        if (exportList != null && exportList.size() > 0){
            for (Export export : exportList) {
                lcNo += export.getLcno() + " ";
                shipmentPort += export.getShipmentPort();
                destinationPort += export.getDestinationPort();
                exportDao.updateByPrimaryKeySelective(export);
                //修改装箱单的状态
                packing.setState(1);
                packingDao.updateByPrimaryKeySelective(packing);
            }
        }
        Shipping shipping = new Shipping();
        //设置委托单的信用证
        shipping.setLcNo(lcNo);
        //设置委托单的装运港
        shipping.setPortOfLoading(shipmentPort);
        //设置委托单的卸货港
        shipping.setPortOfDischar(destinationPort);
        shipping.setShippingOrderId(packing.getPackingListId());

        //Shipping shipping = shippingDao.selectByPrimaryKey(id);
        //shippingDao.insertSelective(shipping);
        return shipping;
    }

    @Override
    public List<Shipping> findAll(ShippingExample example) {
        return shippingDao.selectByExample(example);
    }
}
